package org.hackystat.devcathlon.ui.page.teams.profile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.DevcathlonBasePage;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;
import org.hackystat.devcathlon.ui.page.teams.TeamsSession;

/**
 * Implements the Teams profile page, where it displays information such as 
 * project name, team name, members, bio, etc. 
 * 
 * @author John Ly
 */
public class TeamsProfile extends DevcathlonBasePage {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Creates the Teams Profile page.    
   */
  public TeamsProfile () {
    TeamsSession session = DevcathlonSession.get().getTeamsSession();
    
    // Set the body class attribute to section-1 so that the menubar is correct.
    add(new WebMarkupContainer("body") { 
      private static final long serialVersionUID = 1L;
      @Override
      public boolean isTransparentResolver() { 
        return true; 
        } 
      @Override
      protected void onComponentTag(ComponentTag tag) { 
        tag.put("class","thrColElsHdr section-2"); 
        }
    }); 
    
    // Make the tab corresponding to this page bold.
    get("TeamsLink").add(new SimpleAttributeModifier("class", "selected"));
    
    add(new Label("TeamNameLabel", session.getSelectedTeam()));
    add(new Label("TeamProjectLabel", session.getProjectName()));
    
    // Add the team logo to the page
    try {
      final BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
      BufferedImage image;      
      image = ImageIO.read(new File(session.getTeamLogoPath()));
      resource.setImage(image);
      add(new Image("team_logo", resource)); 
    }
    catch (IOException e) {
      add(new Image("team_logo"));
    }
    
    Link ownerLink = new Link ("OwnerProfileLink") {
      private static final long serialVersionUID = 1L;

      @Override
      public void onClick() {
        TeamsSession session = DevcathlonSession.get().getTeamsSession();
        DevcathlonSession.get().getProfileSession().setProfile(session.getOwner().getEmail());
        setResponsePage(new ProfilePage());
      }          
    };
    
    ownerLink.add(new Label("TeamOwnerLabel", 
        session.getOwner().getFirstName() + " " + session.getOwner().getLastName()));
    add(ownerLink);
    add(new MultiLineLabel("TeamDescriptionLabel", session.getDescription()));
    
    final Team currentTeam = TeamManager.getInstance().getTeam(session.getSelectedTeam());
    List<User> everyone = new ArrayList<User>(currentTeam.getMembers());
    everyone.addAll(currentTeam.getInvited());
    Collections.sort(everyone);
    
    ListView memberList = new ListView("MembersList", everyone) {
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        final User member = (User) item.getModelObject();
        
        Link memberLink = new Link ("MemberProfileLink") {
          private static final long serialVersionUID = 1L;

          @Override
          public void onClick() {
            DevcathlonSession.get().getProfileSession().setProfile(member.getEmail());
            setResponsePage(new ProfilePage());
          }          
        };
        
        String name = member.getFirstName() + " " + member.getLastName();
        if (currentTeam.getOwner().getEmail().equals(member.getEmail())) {
          name = name + "(o)";
        }
        else if (currentTeam.isMember(member.getEmail())) {
          name = name + "(m)";
        }
        else {
          name = name + "(i)";
        }
        memberLink.add(new Label("MemberLabel", name));
        
        item.add(memberLink);
      }      
    };
    
    add(memberList);
    add(new TeamMatchesPanel("TeamMatches"));
  } 

}
